<?php

namespace App\Admin\Actions\Post;

use DB;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;

class RefuseAuditOk extends RowAction
{
    public $name = '驳回';

    public function handle(Model $model)
    {
        if( $model->status == 32 ){
            $model->status = $model->refuse_status;
            $model->refuse_status = 0;
            // $model->fail_reason = $model->status==10?"非本店订单":"评价截图有误";
            

            //如果冻结，检查是否结算完毕，恢复正常状态
            $count = \DB::table('users_orders')->where('store_uid', $model->store_uid)->whereIn('status', [40,35])->where('created_at', '<', date('Y-m-d 00:00:00', strtotime('-1 day', time())))->count();
            $merchantUser = DB::table('users')->where('id', $model->store_uid)->first();
            if( !$count && $merchantUser->frozen_date != null ){
                //已冻结，但是当前无待结算订单，可恢复
                \DB::table('users')->where('id', $model->store_uid)->update(['frozen_date'=>null, 'created_at'=>getDt()]);
                \DB::table('subsidy')->where('uid', $model->store_uid)->update(['is_frozen'=>0, 'created_at'=>getDt()]);
            }
            $model->save();
        }

        return $this->response()->success('已驳回')->refresh();
    }

}